package com.mhkj.supervise.controller;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mhkj.common.enums.PlanType;
import com.mhkj.revise.plan.entity.PlanNode;
import com.mhkj.revise.plan.service.PlanNodeService;
import com.mhkj.supervise.entity.PlanNodeVo;
import com.mics.core.common.componet.Pagination;
import com.mics.core.system.organizational.service.OrgService;
import com.mics.core.system.rbac.entity.Dictionary;
import com.mics.core.system.rbac.service.DictionaryService;

/**
 * 督导
 */

@Controller
@RequestMapping("/supervise")
public class SuperviseController {
	@Autowired
	private DictionaryService dictionaryService;
	@Autowired
	private OrgService orgService;
	@Autowired
	private PlanNodeService planNodeService;
	/**
	 * 分页查询督导
	 * @throws Exception 
	 * 
	 */
	@RequestMapping(value="/listByPage", method=RequestMethod.POST)
	@ResponseBody
	public Pagination<PlanNodeVo> listByPage( Long year,String code,String nodeName,Long itemId,Long month, int pageIndex, int pageSize ) throws   Exception {
		if(StringUtils.isEmpty(code)) {
			return null;
		} 
			Dictionary byCode = dictionaryService.getByCode(code);
			if(StringUtils.isEmpty(byCode)) {
				return null;
			}
			Long planTypeId = byCode.getId();
	
		Pagination<PlanNode> pag = planNodeService.findSupervise(year,planTypeId,code,nodeName,itemId,month,pageIndex,pageSize);
		Pagination<PlanNodeVo> p =new Pagination<PlanNodeVo>();
		p.setTotal(pag.getTotal());
		List<PlanNodeVo> vo = new ArrayList<PlanNodeVo>();
		
		List<PlanNode> data = pag.getData();
		for (PlanNode planNode : data) {
			PlanNodeVo planNodeVo = new PlanNodeVo();
			
			if(code.equals(PlanType.FUNC_TYPE.getCode())) {
				planNodeVo.setBusinessName(planNode.getPlan().getFunctionOffice().getName());
				planNodeVo.setNodePropertyName(planNode.getNodeProperty().getValue());
			}else {
				planNodeVo.setBusinessName(planNode.getBusiness().getTypeName());
			}
			
			String postId = planNode.getPostId();
			if(orgService.getEmployeeByPost(postId).size()>0) {
				planNode.setPostName(orgService.getEmployeeByPost(postId).get(0).getName());
			}
			planNodeVo.setPlanName(planNode.getPlan().getName());
			BeanUtils.copyProperties(planNodeVo, planNode);
			vo.add(planNodeVo);
		}
		p.setData(vo);
		
	
		return p;
	
	}
	

}
